﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Validation;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Coslen.TahorCRM.Domain.Entities.ViewModels.Editing;
using Coslen.TahorCRM.Domain.Entities.ViewModels.Searching;
using Coslen.TahorCRM.Domain.Services.Interfaces;
using Coslen.TahorCRM.UI.www.Controllers;
using Ninject;

namespace Coslen.TahorCRM.UI.www.Areas.Contacts.Controllers
{
    public class ContactRelationshipController : TahorBaseController
    {
        [Inject]
        public IContactRelationshipService ContactRelationshipService { get; set; }

        //
        // GET: /ContactRelationship/
        [Authorize(Roles = "CanEditContact, CanViewContact, CanDeleteContact")]
        public ActionResult Index(ContactRelationshipSearchViewModel model)
        {
            try
            {
                if (model == null)
                {
                    model = new ContactRelationshipSearchViewModel();
                    // any defaults???
                }

                model = ContactRelationshipService.GetContactRelationshipSearchViewModel(model);
                return View(model);
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            throw new ApplicationException("Should  not be able to get here!");
        }

        //
        // GET: /ContactRelationship/Details/5
        [Authorize(Roles = "CanEditContact, CanViewContact, CanDeleteContact")]
        public ActionResult Details(int id)
        {
            try
            {
                ContactRelationshipEditViewModel model = ContactRelationshipService.GetContactRelationshipEditViewModelById(id);
                return View(model);
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            throw new ApplicationException("Should  not be able to get here!");
        }

        //
        // GET: /ContactRelationship/Create
        [Authorize(Roles = "CanEditContact")]
        public ActionResult Create(int primaryContactId, string source = "")
        {
            try
            {
                ContactRelationshipEditViewModel model = ContactRelationshipService.GetNewContactRelationshipEditViewModel(primaryContactId);
                model.source = source;
                model.sourceUrl = ConvertSourceToSourceUrl(source, 0);
                model.ContactRelationship.PrimaryContactId = primaryContactId;

                return View(model);
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            throw new ApplicationException("Should  not be able to get here!");
        }

        //
        // POST: /ContactRelationship/Create
        [HttpPost]
        [Authorize(Roles = "CanEditContact")]
        public ActionResult Create(ContactRelationshipEditViewModel model)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    ContactRelationshipService.SaveContactRelationship(model.ContactRelationship);
                    if (!string.IsNullOrEmpty(model.source))
                    {
                        return RedirectToSource(model.source, model.ContactRelationship.PrimaryContactId);
                    }
                    else
                    {
                        return RedirectToAction("Index");
                    }
                }
                else
                {
                    ProcessModelState(ModelState);
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            return View(model);
        }

        //
        // GET: /ContactRelationship/Edit/5
        [Authorize(Roles = "CanEditContact")]
        public ActionResult Edit(int id, string source = "")
        {
            try
            {
                ContactRelationshipEditViewModel model = ContactRelationshipService.GetContactRelationshipEditViewModelById(id);
                model.source = source;
                model.sourceUrl = ConvertSourceToSourceUrl(source, id);

                return View(model);
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            throw new ApplicationException("Should  not be able to get here!");
        }

        //
        // POST: /ContactRelationship/Edit/5
        [HttpPost]
        [Authorize(Roles = "CanEditContact")]
        public ActionResult Edit(ContactRelationshipEditViewModel model)
        {
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    ContactRelationshipService.SaveContactRelationship(model.ContactRelationship);

                    if (!string.IsNullOrEmpty(model.source))
                    {
                        return RedirectToSource(model.source, model.ContactRelationship.PrimaryContactId);
                    }
                    else
                    {
                        return RedirectToAction("Index");
                    }
                }
                else
                {
                    ProcessModelState(ModelState);
                }

                return RedirectToAction("Index");
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            // TODO: add any alter failed edit model logic here....

            return View(model);
        }

        //
        // GET: /ContactRelationship/Delete/5
        [Authorize(Roles = "CanDeleteContact")]
        public ActionResult Delete(int id, string source = "")
        {
            try
            {
                ContactRelationshipEditViewModel model = ContactRelationshipService.GetContactRelationshipEditViewModelById(id);
                model.source = source;
                model.sourceUrl = ConvertSourceToSourceUrl(source, 0);

                return View(model);
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            throw new ApplicationException("Should  not be able to get here!");
        }

        //
        // POST: /ContactRelationship/Delete/5
        [HttpPost, ActionName("Delete")]
        [Authorize(Roles = "CanDeleteContact")]
        public ActionResult DeleteConfirm(int id)
        {
            try
            {
                // TODO: Add delete logic here
                ContactRelationshipEditViewModel model = ContactRelationshipService.GetContactRelationshipEditViewModelById(id);
                ContactRelationshipService.DeleteContactRelationship(model.ContactRelationship);

                if (!string.IsNullOrEmpty(model.source))
                {
                    return RedirectToSource(model.source, 0);
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                ProcessDbEntityValidationException(dbEx);
            }
            catch (DbUpdateException dbEx)
            {
                ProcessDbUpdateException(dbEx);
            }
            catch (SqlException sqlEx)
            {
                ProcessSqlException(sqlEx);
            }

            return RedirectToAction("Delete", new { id = id });
        }

    }
}
